﻿using System.Windows.Forms;
using LinkDownloader.Core;
using LinkDownloader.Forms;
using LinkDownloader.Forms.JobLists;

namespace LinkDownloader.DownloadManger
{
    internal class SingleDownload : DownloadJob
    {

        public enum State
        {
            Displaying,
            Hidden
        }

        private frmSingleDownload form = null;
        public frmSingleDownload Form
        {
            get { return this.form; }
        }

        private JobListRow row = null;
        public JobListRow Row
        {
            get { return this.row; }
        }

        private State state = State.Hidden;
        public State DisplayState
        {
            get { return this.state; }
            set { this.state = value; }
        }
        
        public SingleDownload(Downloader downloader, int id)
            : base(downloader, id)
        {
            this.form = new frmSingleDownload(this);
        }

        public void Show()
        {
            this.form.Show();
        }

        public void Hide()
        {
            this.form.Hide();
        }

        public void InitializeRow(JobList list)
        {
            if (this.row == null) { this.row = new JobListRow(list, this); }
            this.form.Link = this.Link;
            this.form.SaveDirectory = this.FileDirectory;
        }

        protected override void OnProgressChanged(int percent)
        {
            base.OnProgressChanged(percent);
            this.form.IncrementProgress(percent);
            if (this.row != null) { this.row.List.IncrementProgress(this.row); }
        }

        protected override void OnStatusChanged(ChangedEventArgs e)
        {
            base.OnStatusChanged(e);
            this.form.UpdateStatus(null);
            if (this.row != null) { this.row.List.UpdateStatus(this.row); }
        }

        protected override void OnCompletedSizeChanged(ChangedEventArgs e)
        {
            base.OnCompletedSizeChanged(e);
            this.form.UpdateSize(null);
        }

        protected override void OnTotalSizeChanged(ChangedEventArgs e)
        {
            base.OnTotalSizeChanged(e);
            this.form.UpdateSize(null);
            if (this.row != null) { this.row.List.UpdateSize(this.row); }
        }

        protected override void OnTransferRateChanged(ChangedEventArgs e)
        {
            base.OnTransferRateChanged(e);
            this.form.UpdateTransferRate(null);
            if (this.row != null)
            {
                this.row.List.UpdateTransferRate(this.row);
                this.row.List.UpdateTimeLeft(this.row);
            }
        }

        protected override void OnStarted(ChangedEventArgs e)
        {
            base.OnStarted(e);
            this.form.PerformDownloadStartPause();
        }

        protected override void OnFileChanged(ChangedEventArgs e)
        {
            base.OnFileChanged(e);
            this.form.UpdateFileName(null);
        }

        public bool ChangeSaveDirectory()
        {
            
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.RootFolder = System.Environment.SpecialFolder.Desktop;
            dlg.SelectedPath = this.FileDirectory;
            dlg.ShowNewFolderButton = true;
            dlg.Description = "Select the directory in which to save the file.";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.FileDirectory = dlg.SelectedPath;
                return true;
            }
            return false;

        }

    }
}
